//
//  CYBindSuccess.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/7/13.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYSuccessTipsView.h"
@interface CYSuccessTipsView(){
    tipsBlock _myCallBlock;
}
@property (nonatomic, strong) CYLabel *titleLBL;
@property (nonatomic, strong) CYImageView *IMG;
@property (nonatomic, strong) CYView *line;
@property (nonatomic, strong) CYButton *toDrawCashBtn;
@property (nonatomic, copy) NSString *title;//
@property (nonatomic, copy) NSString *buttonTitle;//按钮title
@end
@implementation CYSuccessTipsView
- (instancetype)initWithTitle:(NSString *)title buttonTitle:(NSString *)buttonTitle block:(tipsBlock)callback{
    self = [super init];
    if (self) {
        _myCallBlock = callback;
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = AL(5.0);
        self.IMG.hidden = NO;
        self.title = title;
        self.buttonTitle = buttonTitle;
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}
+ (void)showTitle:(NSString *)title buttonTitle:(NSString *)buttonTitle block:(tipsBlock)block{
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [window makeKeyAndVisible];
    
    CYView *alphaView = [[CYView alloc]initWithColor:TEXT_BLACK_COLOR];
    alphaView.alpha = 0.3;
    alphaView.tag = 300;
    [window addSubview:alphaView];
    
    [alphaView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    CYSuccessTipsView *tipsView = [[CYSuccessTipsView alloc]initWithTitle:title buttonTitle:buttonTitle block:block];
    tipsView.tag = 301;
    [window addSubview:tipsView];
    [tipsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(window);
        make.size.mas_equalTo(CGSizeMake(AL(PT(520)), AL(PT(310))));
    }];
    
}
- (CYImageView *)IMG{
    if (!_IMG) {
        _IMG = [[CYImageView alloc]initWithImageName:@"Success" size:CGSizeMake(AL(PT(80)), AL(PT(80))) isRadius:NO];
        [self addSubview:_IMG];
        
        [_IMG mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.centerY.equalTo(self).with.offset(-AL(PT((310 - 80 )/ 2.0 - 50)));
            make.size.mas_equalTo(_IMG.bounds.size);
        }];
    }
    return _IMG;
}

- (CYLabel *)titleLBL{
    if (!_titleLBL) {
        _titleLBL = [[CYLabel alloc]initWithText:_title size:AL(PT(28)) weight:UIFontWeightLight];
        _titleLBL.textColor = TEXT_BLACK_COLOR;
        [self addSubview:_titleLBL];
        
        [_titleLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_IMG);
            make.top.equalTo(_IMG.mas_bottom).with.offset(AL(PT(29)));
            make.size.mas_equalTo(_titleLBL.bounds.size);
        }];
    }
    return _titleLBL;
}

- (CYView *)line{
    if (!_line) {
        _line = [[CYView alloc]initWithColor:ColorHex(0xe3e3e3)];
        [self addSubview:_line];
        
        [_line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(_IMG);
            make.top.equalTo(_titleLBL.mas_bottom).with.offset(AL(PT(18)));
            make.size.mas_equalTo(CGSizeMake(AL(PT(520 - 80)), 0.5));
        }];
    }
    return _line;
}

- (CYButton *)toDrawCashBtn{
    if (!_toDrawCashBtn) {
        _toDrawCashBtn = [[CYButton alloc]initWithTitle:_buttonTitle titleColor:TEXT_BLACK_COLOR backgroundColor:[UIColor whiteColor] cornerRadius:AL(5.0)];
        _toDrawCashBtn.titleLabel.font = [UIFont systemFontOfSize:AL(PT(32)) weight:UIFontWeightBold];
        _toDrawCashBtn.layer.borderColor = LINE_COLOR.CGColor;
        _toDrawCashBtn.layer.borderWidth = 0.5;
        [_toDrawCashBtn addTarget:self action:@selector(acitonForToDrawCashBtn) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_toDrawCashBtn];
        
        [_toDrawCashBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self);
            make.centerY.equalTo(self).with.offset(AL(PT(310 - 100) / 2.0));
            make.size.mas_equalTo(CGSizeMake(AL(520 - 80), AL(PT(100))));
        }];
    }
    return _toDrawCashBtn;
}

- (void)setTitle:(NSString *)title{
    if (_title != title)
        _title = title;
    self.titleLBL.hidden  = NO;
    
}

- (void)setButtonTitle:(NSString *)buttonTitle{
    if (_buttonTitle != buttonTitle)
        _buttonTitle = buttonTitle;
    self.toDrawCashBtn.hidden = NO;
    self.line.hidden = NO;
    
}

- (void)acitonForToDrawCashBtn{
    _myCallBlock();
    for (CYView *view in [self.superview subviews]) {
        if (view.tag == 300  || view.tag == 301)
            [view removeFromSuperview];
    }
    
}
/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end
